package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.VCloudInformation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.Expression;

/**
 * 学校信息管理控制类
 * @author 徐震霖
 * @since 2018-12-13
 */
@Controller
@RequestMapping(value = "/cloudinformation")
public class SysCloudInformationManagementController {

	/**
	 * 加载云平台发布可公开的信息
	 * 
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	// TODO 加载云平台发布可公开的信息
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "查询云平台发布可公开的信息", operationClassPath = "controller.SysCloudInformationManagementController.getSelectHomePageInformaction(Model model,HttpServletRequest req, HttpServletResponse resp)", operationMenu = "网站信息管理/云平台信息推送")
	@RequestMapping(value = "/getselhomepageinformaction")
	public String getSelectHomePageInformaction(Model model, HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		Expression exp = new Expression();
		JSONObject result = new JSONObject();

		try {
			int currentPage = 1;
			int pageSize = 10;

			String infotype = req.getParameter("infotype");
			String schoolid = req.getParameter("schoolid");
			String title = req.getParameter("title");
			String limit = req.getParameter("limit");
			String page = req.getParameter("page");
			String strWhere = "";

			exp.andEqu("isPublic", Constant.ISPUBLIC, Integer.class);
			if (limit != null && !limit.equals("") && !limit.equals("0")) {// 每页条数
				pageSize = Integer.parseInt(limit);
			}
			if (page != null && !page.equals("") && !page.equals("0")) {// 当前页
				currentPage = Integer.parseInt(page);
			}
			if (schoolid != null && !schoolid.equals("") && !schoolid.equals("0")) {
				exp.andEqu("schoolid", schoolid, String.class);
			}
			if (title != null && !title.equals("")) {
				exp.andLike("topic", title, String.class);
				// exp.orLike("realName", title, String.class);
			}
			if (infotype != null && !infotype.equals("") && !infotype.equals("0")) {
				exp.andEqu("type", infotype, Integer.class);
				strWhere = exp.toString();
			} else {
				// exp.andEqu("type", 1003, Integer.class);
				// exp.orEqu("type", 1002, Integer.class);
				strWhere = exp.toString() + " and (type = 1003 or type = 1002)";
			}

			List<VCloudInformation> listsize = DAOFactory.getCloudInformationDAO().getVCloudInformationByPage(strWhere);
			List<VCloudInformation> list = DAOFactory.getCloudInformationDAO().getVCloudInformationByPage(strWhere,
					currentPage, pageSize);
			if (list != null && list.size() > 0) {
				result.put("code", 0);
				result.put("msg", "查询成功！");
				result.put("count", listsize.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			} else {
				result.put("code", 1);
				result.put("msg", "未获取到数据！");
			}
		} catch (Exception e) {
			result.put("code", 1);
			result.put("msg", "出现异常，查询失败，请重试！");
			e.printStackTrace();
		}

		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}

}
